package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.Registlevel;
import com.foreknow.entity.User;
import com.foreknow.service.impl.RegisLevelServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/registlevel")
public class RegistLevelController {
    @Autowired RegisLevelServiceImpl regisLevelService;


    @RequestMapping(value = "queryAll",method = RequestMethod.GET)
    public String queryAll(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();

        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize =Integer.parseInt((String)  para.get("pageSize"));
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);

        List<Registlevel> registlevels = regisLevelService.queryAll();

        //PageInfo是PageHelper中内置的分页信息类
        PageInfo<User> info = new PageInfo(registlevels);
        //给map添加数据
        hs.put("total",info.getTotal());
        hs.put("pageNum",info.getPageNum());
        hs.put("list",registlevels);
        if (registlevels.size()>0){
            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("获取失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }



    //添加挂号等级
    @RequestMapping(value = "insertReg",method = RequestMethod.POST)
    public String insertReg(@RequestBody Registlevel registlevel) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        registlevel.setRegistquota(100);
        int isRight = regisLevelService.insertReg(registlevel);
        hs.put("reg",registlevel);
        if (isRight==1){
            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("获取失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //根据id删除挂号等级
    @RequestMapping(value = "deleteById",method = RequestMethod.GET)
    public String deleteById(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        Integer id = Integer.parseInt((String)para.get("id"));
        int isRight = regisLevelService.deleteById(id);
        if (isRight==1){
            hs.put("meta",new Constance("删除成功",200));
        }else {
            hs.put("meta",new Constance("删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //批量删除用户信息
    @RequestMapping(value = "deleteByChoose",method = RequestMethod.POST)
    public String deleteByChoose(@RequestBody Map<String ,Object>para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        List<Integer> arr = new ArrayList<Integer>();
        arr= (List<Integer>) para.get("info");
        int sum =0;
        int isRight =0;
        for(Integer ss:arr
        ) {
            isRight =regisLevelService.deleteById(ss);
            sum = sum + isRight;
        }
        if (sum==arr.size()){
            hs.put("meta",new Constance("批量删除成功",200));
        }else {
            hs.put("meta",new Constance("批量删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    //根据id查询用户信息
    @RequestMapping(value = "selectUserById",method = RequestMethod.GET)
    public String selectUserById(@RequestParam Map<String,Object>para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        Integer id =Integer.parseInt((String) para.get("id"));
        Registlevel registlevel = regisLevelService.selectById(id);
        hs.put("registlevel",registlevel);
        if (registlevel!=null){
            hs.put("meta",new Constance("根据id获取用户信息成功",200));
        }else {
            hs.put("meta",new Constance("根据id获取用户信息失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }


    //根据id修改用户信息
    @RequestMapping(value = "updateReg" ,method = RequestMethod.POST)
    public String updateUserById(@RequestBody Registlevel registlevel) throws JsonProcessingException {
        //获取表单信息
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
//        String registcode = (String) para.get("registcode");
//        String registname = (String) para.get("registname");
//        Integer sequenceno  = (Integer) para.get("sequenceno");
//       Integer registfee= (Integer) para.get("registfee");
//       BigDecimal bigDecimal = new BigDecimal(registfee.toString());
//        Integer depID  = (Integer) para.get("id");
//
//        Registlevel registlevel =new Registlevel();
//        registlevel.setDelmark(1);
//        registlevel.setId(depID);
//        registlevel.setRegistcode(registcode);
//        registlevel.setRegistname(registname);
//        registlevel.setSequenceno(sequenceno);
//        registlevel.setRegistfee(registfee);

        //调用service修改方法
        int isRight =  regisLevelService.updateByReg(registlevel);
        if (isRight==1){
            hs.put("meta",new Constance("修改用户成功",200));
        }else {
            hs.put("meta",new Constance("修改用户失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }

    //根据条件查询信息
    @RequestMapping(value = "queryReg",method = RequestMethod.POST)
    @ResponseBody
    public String queryUser(@RequestBody Registlevel registlevel) throws JsonProcessingException{
        HashMap<String,Object> hs=new HashMap<>();
        //获取请求参数

        List<Registlevel> registlevels = regisLevelService.selectByChoose(registlevel);


        hs.put("registlevels",registlevels);

        //添加meta
        if (registlevels.size()>0){
            hs.put("meta",new Constance("查询成功",200));
        }else {
            hs.put("meta",new Constance("查询失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
